package com.example.yichuangyun.mvvm.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemCallTextBinding;
import com.example.yichuangyun.model.call.AliSentenceModel;
import com.example.yichuangyun.utils.DensityUtil;

public class AliCallTextAdapter extends BaseQuickAdapter<AliSentenceModel, DataBindingHolder<ItemCallTextBinding>> {

    private Context context;

    public AliCallTextAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemCallTextBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_call_text, viewGroup);
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemCallTextBinding> itemUsersBindingDataBindingHolder, int i, @Nullable AliSentenceModel model) {
        ItemCallTextBinding binding = itemUsersBindingDataBindingHolder.getBinding();
        if (model != null) {
//            String text = !TextUtils.isEmpty(model.getText()) ?
//                    model.getText()
//                            .replaceAll("<\\|Speech\\|>", "<div style=\"margin-bottom:10px\"></div><span>")
//                            .replaceAll("<\\|/Speech\\|>", "</span>")
//                            .replaceAll("<\\|NEUTRAL\\|>", "<img src=\"https://app-pic.yikaiye.net/h5/NEUTRAL.png?v=1\" style=\"width:15px;height:15px\"/>")
//                            .replaceAll("<\\|ANGRY\\|>", "<img src=\"https://app-pic.yikaiye.net/h5/ANGRY.png?v=1\" style=\"width:15px;height:15px\"/>")
//                            .replaceAll("<\\|HAPPY\\|>", "<img src=\"https://app-pic.yikaiye.net/h5/HAPPY.png?v=1\" style=\"width:15px;height:15px\"/>")
//                            .replaceAll("<\\|SAD\\|>", "<img src=\"https://app-pic.yikaiye.net/h5/SAD.png?v=1\" style=\"width:15px;height:15px\"/>")
//                            .replaceAll("<\\|Laughter\\|>", "<div style=\"margin-bottom:10px\"></div><span>")
//                            .replaceAll("<\\|/Laughter\\|>", "</span>")
//                            // 替换其他标签类似<|xxx|>和<|/xxx|>为空字符串
//                            .replaceAll("<\\|.*?\\|>", "")
//                            .replaceAll("<\\|/.*?\\|>", "")
//                    : "";
//            binding.text.setText(text);
//            binding.text.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY));

//            String text = !TextUtils.isEmpty(model.getText()) ?
//                    model.getText()
//                            .replaceAll("<\\|Speech\\|>", "<img src='https://app-pic.yikaiye.net/h5/NEUTRAL.png?v=1'>")
//                            .replaceAll("<\\|/Speech\\|>", "</span>")
////                            .replaceAll("<\\|NEUTRAL\\|>", "<img src=\"https://app-pic.yikaiye.net/h5/NEUTRAL.png?v=1\"/>")
////                            .replaceAll("<\\|ANGRY\\|>", "<img src=\"https://app-pic.yikaiye.net/h5/ANGRY.png?v=1\"/>")
////                            .replaceAll("<\\|HAPPY\\|>", "<img src=\"https://app-pic.yikaiye.net/h5/HAPPY.png?v=1\"/>")
////                            .replaceAll("<\\|SAD\\|>", "<img src=\"https://app-pic.yikaiye.net/h5/SAD.png?v=1\"/>")
////                            .replaceAll("<\\|Laughter\\|>", "")
////                            .replaceAll("<\\|/Laughter\\|>", "</span>")
////                            // 替换其他标签类似<|xxx|>和<|/xxx|>为空字符串
////                            .replaceAll("<\\|.*?\\|>", "")
////                            .replaceAll("<\\|/.*?\\|>", "")
//                    : "";
//
//            // 2. 使用 Html.fromHtml 解析
//            Spanned spannedText = Html.fromHtml(
//                    text,
//                    Html.FROM_HTML_MODE_LEGACY,
//                    new UrlImageGetter(binding.text), // 自定义 ImageGetter
//                    null
//            );
//
//            // 3. 设置到 TextView
//            binding.text.setText(spannedText);

            String originalText = model.getText();
            int image;
            if (originalText.contains("<|NEUTRAL|>")) {
                image = R.mipmap.logo;
            } else if (originalText.contains("<|ANGRY|>")) {
                image = R.mipmap.ic_add_image;
            } else if (originalText.contains("<|HAPPY|>")) {
                image = R.mipmap.ic_add_image;
            } else if (originalText.contains("<|SAD|>")) {
                image = R.mipmap.ic_add_image;
            } else {
                image = -1;
            }

            String text = originalText.replaceAll("<\\|Speech\\|>", "")
                    .replaceAll("<\\|/Speech\\|>", "")
                    .replaceAll("<\\|NEUTRAL\\|>", "")
                    .replaceAll("<\\|ANGRY\\|>", "")
                    .replaceAll("<\\|HAPPY\\|>", "")
                    .replaceAll("<\\|SAD\\|>", "")
                    .replaceAll("<\\|Laughter\\|>", "")
                    .replaceAll("<\\|/Laughter\\|>", "")
                    // 替换其他标签类似<|xxx|>和<|/xxx|>为空字符串
                    .replaceAll("<\\|.*?\\|>", "")
                    .replaceAll("<\\|/.*?\\|>", "");

//            SpannableString spannable = new SpannableString(text);
//            if (image != -1) {
//                Drawable drawable = ContextCompat.getDrawable(context, image);
//                if (drawable != null) {
//                    drawable.setBounds(0, 0, DensityUtil.dip2px(context, 10), DensityUtil.dip2px(context, 10));
//                    ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
//                    spannable.setSpan(imageSpan, spannable.length() - 1, spannable.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//                }
//            }
//            binding.text.setText(spannable);
            binding.text.setText(text);
        }
    }
}
